/*
通用OTDR

2021.7.28 by Menglong Wu
*/
#include "port.h"
#include "fpga-common.h"
#include "fpga-private.h"
#include "interface/protocal-otdr.h"

struct otdr_driver_data_tr700
{
    char	 *otdr_cache;
    unsigned char ch;
    unsigned long checksum;  ///< OTDR
};


static int _this_cb_fpga_otdr_probe(struct fpga_dev *dev)
{

    struct priv_otdr_data         *pdata_bsp = fpga_if_get_bsp_data(dev->interface);
    struct otdr_driver_data_tr700 *pnew      = malloc(sizeof(struct otdr_driver_data_tr700));

    printf("\r\nI'm _this_cb_fpga_otdr_probe    from   this-x86-otdr-123k\r\n");
    printf("pdata_bsp->cache_len %d\r\n", pdata_bsp->cache_len);
    pnew->otdr_cache = (char *)malloc(pdata_bsp->cache_len);

    fpga_if_set_driver_data(dev->interface, pnew);
    return 0;
}
int _this_cb_fpga_otdr_release(struct fpga_dev *dev)
{
    struct otdr_driver_data_tr700 *pdata_drv = fpga_if_get_driver_data(dev->interface);
    if (pdata_drv->otdr_cache) {
	free(pdata_drv->otdr_cache);
	pdata_drv->otdr_cache = NULL;
    }
    return 0;
}

static struct priv_otdr_data data_32k = {
    .cache_len   = 32000 * 4 + 4000,
    .sample_byte = 32 * 1000 * 4,
};

static struct fpga_if otdr_interface_32k = {
    DEFAULT_FPGA_IF,
    .pfun_probe      = cb_fpga_otdr_probe,  // common
    .pfun_release    = cb_fpga_otdr_release,
    .private_data    = &data_32k,
    .name            = "this-x86-otdr-32k",
    .pfun_start      = cb_fpga_otdr_start,
    .pfun_isfinish   = cb_fpga_otdr_isfinish,
    .pfun_stop       = cb_fpga_otdr_stop,
    .pfun_set_gpio   = none_set_gpio,
    .pfun_get_gpio   = none_get_gpio,
    .pfun_version    = com_fpga_version,
    .pfun_read_frame = cb_fpga_read_otdr_data,
    .pfun_write_regs = cb_fpga_otdr_write_regs,
};


static struct priv_otdr_data data_123k = {
    .cache_len   = 32000 * 4 + 4000 + 123,
    .sample_byte = 32 * 1000 * 4,
};

static struct fpga_if otdr_interface_123k = {
    DEFAULT_FPGA_IF,
    .pfun_probe      = _this_cb_fpga_otdr_probe,  // local
    .pfun_release    = _this_cb_fpga_otdr_release,
    .private_data    = &data_123k,
    .name            = "this-x86-otdr-123k",
    .pfun_start      = cb_fpga_otdr_start,
    .pfun_isfinish   = cb_fpga_otdr_isfinish,
    .pfun_stop       = cb_fpga_otdr_stop,
    .pfun_set_gpio   = none_set_gpio,
    .pfun_get_gpio   = none_get_gpio,
    .pfun_version    = com_fpga_version,
    .pfun_read_frame = cb_fpga_read_otdr_data,
    .pfun_write_regs = cb_fpga_otdr_write_regs,
};


static void __attribute__((constructor(INIT_BSP))) this_init()
{
    fpga_if_register(&otdr_interface_32k);
    fpga_if_register(&otdr_interface_123k);
}
